﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ASPNETMVCApplication.ActionServiceReference;

namespace ASPNETMVCApplication.Areas.Shop.Models
{
    /// <summary>
    /// Static class that maps Product and Cart Model objects to 
    /// Data Transfer Objects (DTOs) and vice versa.
    /// This class contains extension methods only.
    /// </summary>
    public static class Mapper
    {
        public static CustomerModel ToModel(this Customer customer)
        {
            return new CustomerModel
            {
                CustomerId = customer.CustomerId,
                CompanyName = customer.CompanyName,
                Name = customer.Name,
                City = customer.City,
                Country = customer.Country,
                UserName = customer.UserName,
                Password = customer.Password,
                Email = customer.Email,
                Telephone = customer.Telephone,
                MobilePhone = customer.MobilePhone,
                YahooId = customer.YahooId,
                SkypeId = customer.SkypeId,
                GoogleId = customer.GoogleId,
                FacebookId = customer.FacebookId,
                Status = customer.Status,
            };
        }

        public static Customer FromModel(this CustomerModel customerModel)
        {
            return new Customer
            {
                CustomerId = customerModel.CustomerId,
                CompanyName = customerModel.CompanyName,
                Name = customerModel.Name,
                City = customerModel.City,
                Country = customerModel.Country,
                UserName = customerModel.UserName,
                Password = customerModel.Password,
                Email = customerModel.Email,
                Telephone = customerModel.Telephone,
                MobilePhone = customerModel.MobilePhone,
                YahooId = customerModel.YahooId,
                SkypeId = customerModel.SkypeId,
                GoogleId = customerModel.GoogleId,
                FacebookId = customerModel.FacebookId,
                Status = customerModel.Status,
            };
        }

        public static List<CustomerModel> ToModel(this List<Customer> customers)
        {
            var models = new List<CustomerModel>();
            if (customers != null && customers.Count > 0)
                customers.ForEach(s => models.Add(s.ToModel()));

            return models;
        }
        /// <summary>
        /// Maps product DTO to product Model.
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public static ProductModel ToModel(this Product product)
        {
            return new ProductModel
            {
                ProductId = product.ProductId,
                //CategoryId = product.Category != null ? product.Category.CategoryId : -1,
                CategoryId =  product.CategoryId,
                //ProductName = product.ProductName,
                UnitPrice = string.Format("{0:c}", product.UnitPrice),
                //Weight = product.Weight,                
                //UnitsInStock = product.UnitsInStock,
                //StatusId = product.StatusId 
                StatusId = product.Status
            };
        }

        /// <summary>
        /// Maps list of product DTOs to list of product Models.
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public static List<ProductModel> ToModel(this List<Product> products)
        {
            var models = new List<ProductModel>();
            if (products != null && products.Count > 0)
              products.ForEach(c => models.Add(c.ToModel()));

            return models;
        }

        /// <summary>
        /// Maps shopping cart item DTO to shopping cart item Model.
        /// </summary>
        /// <param name="cartItem"></param>
        /// <returns></returns>
        public static CartItemModel ToModel(this ShoppingCartItem cartItem)
        {
            return new CartItemModel
            {
                CartItemId = cartItem.Id, 
                ProductId = cartItem.Id, 
                ProductName = cartItem.Name, 
                Quantity = cartItem.Quantity, 
                TotalPrice = string.Format("{0:c}", cartItem.UnitPrice * cartItem.Quantity), 
                UnitPrice = string.Format("{0:c}", cartItem.UnitPrice) 
            };
        }

        /// <summary>
        /// Maps Shopping Cart DTO to shopping cart Model.
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public static CartModel ToModel(this ShoppingCart cart)
        {
            var items = new List<CartItemModel>();
            if (cart != null && cart.CartItems != null && cart.CartItems.Length > 0)
               cart.CartItems.ToList().ForEach(i => items.Add(i.ToModel()));

            return new CartModel { CartItems = items, Shipping = string.Format("{0:c}", cart.Shipping), ShippingMethod = cart.ShippingMethod, SubTotal = string.Format("{0:c}", cart.SubTotal), Total = string.Format("{0:c}", cart.Total) };
        }

        // TAIPM : 02/01/2011
        /// <summary>
        /// Maps product Model to product DTO.
        /// Note :
        /// 1. 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static Product FromModel(this ProductModel model)
        {
            Product p = new Product();
            //p.ProductId = model.ProductId;
            //p.ProductName = model.ProductName;
            p.UnitPrice = double.Parse(model.UnitPrice.Trim());
            //p.UnitsInStock = model.UnitsInStock;
            //p.Weight = model.Weight;
            p.Version = model.Version;
            p.Status = model.StatusId;

            Category c = new Category();
            c.CategoryId = model.CategoryId;
            //p.Category = c;
            p.CategoryId = c.CategoryId;
            return p;                               
        }
    }
}